package edu.gmu.project.swe;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



public class socketServer {

	private ServerSocket serverSocket; //Server Socket
    private int port;
    private ExecutorService pool;
    
    public socketServer(int port) {
        this.port = port;
        pool = Executors.newFixedThreadPool(5);
    }
    
    public void start() throws IOException {
        System.out.println("Starting the socket server at port:" + port);
        serverSocket = new ServerSocket(port);

        Socket client = null;
        
        while(true){
        	System.out.println("Waiting for clients...");
        	client = serverSocket.accept();
        	System.out.println("The following client has connected:"+client.getInetAddress().getCanonicalHostName());
        	//A client has connected to this server. Send welcome message
            pool.submit(new SocketClientHandler(client));
           
        }     
    }
    
    public static void main(String []args){
    	socketServer servSocket = new socketServer (9994);
    	try {
			servSocket.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
    
    
    
    /*public void runServer()  {
    try{
    	
        System.out.println("Starting the socket server at port:" + port);
        serverSocket = new ServerSocket(port);
        
        while(true)
        {
        	try
        	{
        		waitForConnection(); //Listen for clients. Block till one connects
    			System.out.println("Waiting for clients...");
    			getStreams(); //get input output streams
    			processConnection(); 
        		//Socket client = serverSocket.accept();
        	}//end try
        	catch (EOFException eofException)
        	{
        		System.out.println("Server Terminated");
    		}//end catch
    		finally
    		{
    			closeConnection(); //close connection
    			++counter;
    		}//end finally
		}//end while
		
        //A client has connected to this server. Send welcome message
        //sendWelcomeMessage(client);
    }//end try
    catch (IOException e)
    {
    	e.printStackTrace();
	}//end catch
	
}//end runServer()


public void waitForConnection()
{
	socketServer socketServer = accept();
}


    private void sendWelcomeMessage(Socket client) throws IOException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        writer.write("Hello. You are connected to a Simple Socket Server. What is your name?");
        writer.flush();
        writer.close();
    }
    
    public static void main(String[] args) {
        // Setting a default port number.
        int portNumber = 9984;
        
        try {
            // initializing the Socket Server
            socketServer socketServer = new socketServer(portNumber);
            socketServer.start();
            
            } catch (IOException e) {
            e.printStackTrace();
        }
    }
}*/